package trading_strategy.events.history;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Created by IntelliJ IDEA.
 * User: gderoujoux
 * Date: 4 juin 2010
 * Time: 18:09:45
 * To change this template use File | Settings | File Templates.
 */
public class AggregateReader {
    TRTHInstrumentReader[] readers;

    public AggregateReader(TRTHInstrumentReader[] readers) {
        this.readers = readers;
    }

    private boolean hasNext() {
        Arrays.sort(readers, new Comparator<TRTHInstrumentReader>() {
            @Override
            public int compare(TRTHInstrumentReader o1, TRTHInstrumentReader o2) {
                long ts1 = o1.nextTimestamp();
                long ts2 = o2.nextTimestamp();
                if (ts1 <= 0 || ts1 > ts2) {
                    return 1;
                } else if (ts2 <= 0 || ts1 < ts2) {
                    return -1;
                }
                return 0;
            }
        });
        return readers[0].hasNext();

    }

    public void read() {
        while(hasNext()) {
            readers[0].readNext();
        }
    }
}
